<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_daily_report_detail extends CI_Controller {

	public $data;
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Admin_daily_report_detail_model');
	}

	public function index($id)
	{
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		if(null == $id)
			redirect('daily_report');	

		$this->dlp_id = $id;
		//get data from company table
		$cond = array(
			'dt.dlr_id' => $id
		);

		$daily_report_details = $this->Admin_daily_report_detail_model->get_all($cond);
		$userdata = $this->session->userdata('userInfo');

		$data = array(
			'title' => 'Khai nghiệp | Thu chi hang ngay',
			'name' => $userdata['name'],
 			'daily_report_details' => $daily_report_details,
 			'dlp_id' => $id
		);

		$this->load->view('tmpl/header', $data);
		$this->load->view('tmpl/left', $data);
		$this->load->view('admin_daily_report_detail_view', $data);
		$this->load->view('tmpl/footer');
	}

	public function add_new()
	{	
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
	
		//get data
		$dlp_id = $_POST['dlp_id'];
		$earn_name = $_POST['earn_name'];
		$earn_value = $_POST['earn_value'];
		$spend_name = $_POST['spend_name'];
		$spend_value = $_POST['spend_value'];

		if((null != $earn_name) && (null != $earn_value) && (null != $spend_name) && (null != $spend_value)) {//if data not null

			$new_data = array(
				'earn_name' => $earn_name,
				'earn_value' => $earn_value,
				'spend_name' => $spend_name,
				'spend_value' => $spend_value,
				'dlr_id' => $dlp_id
			);

			$id = $this->Admin_daily_report_detail_model->add_new($new_data); //id = -1: fail, else 
			//success

			if($id != -1) { //insert success
				$rp = array('id' => $id);
				echo json_encode($rp);
			}else
				return false;
		}else
			return false;
	}

	//edit
	public function edit()
	{	
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data
		$id = $_POST['id'];
		$earn_name = $_POST['earn_name'];
		$earn_value = $_POST['earn_value'];
		$spend_name = $_POST['spend_name'];
		$spend_value = $_POST['spend_value'];

		if((null != $id) && (null != $earn_name) && (null != $earn_value) && (null != $spend_name) && (null != $spend_value)) {//if data not null
			$cond = array(
				'id' => $id
			);

			$new_data = array(
				'earn_name' => $earn_name,
				'earn_value' => $earn_value,
				'spend_name' => $spend_name,
				'spend_value' => $spend_value
			);
 
			//success
			if($this->Admin_daily_report_detail_model->edit($new_data, $cond)) { //update success
				return true;
			}else
				return false;
		}else
			return false;
	}

	//delete
	public function delete()
	{
		//check if logged in
		if(null == $this->session->userdata('userInfo')) //if not login
			redirect('login');

		if(null != $this->session->userdata('loginError'))
			$this->session->unset_userdata('loginError');
		
		//get data
		$id = $_POST['id'];
		if(null != $id) {
			$cond = array(
				'id' => $id
			);
			
			if($this->Admin_daily_report_detail_model->delete($cond)) { //delete success
				return true;
			}else
				return false;

		}else 
			return false;
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */